'use strict';
/**
 * Created by FreezeSoul on 2015/9/30.
 */
define([
        'angular',
        'app/modules/page/define'
    ],
    function (angular) {
        angular.module('ad.page')
            .controller('adPageLayoutFreeContainerController', ['$scope', '$state', '$uibModalInstance', 'item', function ($scope, $state, $uibModalInstance, item) {
                $scope.item = item;

                $scope.sizeType = [
                    {value: 'percent', text: '百分比'},
                    {value: 'pixel', text: '像素'}
                ];

                $scope.siteType = [
                    {value: 'percent', text: '百分比'},
                    {value: 'pixel', text: '像素'}
                ];

                $scope.editChart = function () {
                    $state.go('editor.index-edit', {chartid: item.chartId});
                    $uibModalInstance.dismiss('cancel');
                };

                var checkIsInValid = function (value) {
                    return value === "" || isNaN(value);
                };

                $scope.ok = function () {
                    if ($scope.item.isEnableLeft && $scope.item.isEnableRight) {
                        $scope.item.width = Number.NaN;
                    } else {
                        if (checkIsInValid($scope.item.width)) {
                            return;
                        }
                    }
                    if ($scope.item.isEnableTop && $scope.item.isEnableBottom) {
                        $scope.item.height = Number.NaN;
                    } else {
                        if (checkIsInValid($scope.item.height)) {
                            return;
                        }
                    }
                    if ($scope.item.isEnableLeft) {
                        if (checkIsInValid($scope.item.left)) {
                            return;
                        }
                    } else {
                        $scope.item.left = Number.NaN;
                    }
                    if ($scope.item.isEnableRight) {
                        if (checkIsInValid($scope.item.right)) {
                            return;
                        }
                    } else {
                        $scope.item.right = Number.NaN;
                    }
                    if ($scope.item.isEnableTop) {
                        if (checkIsInValid($scope.item.top)) {
                            return;
                        }
                    } else {
                        $scope.item.top = Number.NaN;
                    }
                    if ($scope.item.isEnableBottom) {
                        if (checkIsInValid($scope.item.bottom)) {
                            return;
                        }
                    } else {
                        $scope.item.bottom = Number.NaN;
                    }
                    if (!$scope.item.isEnableLeft && !$scope.item.isEnableRight) {
                        return;
                    }
                    if (!$scope.item.isEnableTop && !$scope.item.isEnableBottom) {
                        return;
                    }
                    $uibModalInstance.close($scope.item);
                };

                $scope.cancel = function () {
                    $uibModalInstance.dismiss('cancel');
                };
            }]);
    });